package edu.bu.grocerylist.dao;

import java.util.List;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import edu.bu.grocerylist.model.GroceryList;
import edu.bu.grocerylist.model.GroceryUser;

/**
 * Data Access Object used to perform CRUD actions for GroceryList objects
 * @author donlanp
 *
 */
public class GroceryListDao extends SqlSessionDaoSupport implements IGroceryListDao {

	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryListDao#saveGroceryList(edu.bu.grocerylist.model.GroceryList)
	 */
	public GroceryList saveGroceryList(GroceryList list){
		if(list.getGroceryListId() == 0){
			//Insert
			getSqlSession().insert("insertGroceryList", list);
		} else {
			//Update
			getSqlSession().update("updateGroceryList", list);
		}
		return list;
	}
	
	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryListDao#deleteGroceryList(edu.bu.grocerylist.model.GroceryList)
	 */
	public boolean deleteGroceryList(GroceryList list){
		int rows = getSqlSession().delete("deleteGroceryList", list);
		return rows == 1;
	}
	
	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryListDao#getGroceryListsByUser(java.lang.String)
	 */
	public List<GroceryList> getGroceryListsByUser(int userId){
		return getSqlSession().selectList("selectGroceryListByUserId", userId);
	}
	
	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryListDao#getGroceryListById(int)
	 */
	public GroceryList getGroceryListById(int id){
		return getSqlSession().selectOne("selectGroceryListById", id);
	}

	public void deleteGroceryListsOfUser(GroceryUser user) {
		getSqlSession().delete("deleteGroceryListsOfUSer", user);
	}
	
}
